Test Series - Data Structure

Test Number 40/115

Q: What does the number of inversions in an array indicate?
A. mean value of the elements of array
B. measure of how close or far the array is from being sorted
C. the distribution of values in the array
D. median value of the elements of array
Solution: The number of inversions in an array indicates how close or far the array is from being completely sorted. The array is sorted if the number of inversions are 0.
Q: How many inversions does a sorted array have?
A. 0
B. 1
C. 2
D. cannot be determined
Solution: When an array is sorted then there cannot be any inversion in the array. As the necessary condition for an inversion is arr[i]>arr[j] and i
Q: What is the condition for two elements arr[i] and arr[j] to form an inversion?
A. arr[i]
B. i < j
C. arr[i] < arr[j] and i < j
D. arr[i] > arr[j] and i < j
Solution: For two elements to form an inversion the necessary condition is arr[i] > arr[j] and i < j. The number of inversions in an array indicate how close or far the array is from being completely sorted.
Q: Under what condition the number of inversions in an array are maximum?
A. when the array is sorted
B. when the array is reverse sorted
C. when the array is half sorted
D. depends on the given array
Solution: Number of inversions in an array are maximum when the given array is reverse sorted. As the necessary condition for an inversion is arr[i]>arr[j] and i
Q: Under what condition the number of inversions in an array are minimum?
A. when the array is sorted
B. when the array is reverse sorted
C. when the array is half sorted
D. depends on the given array
Solution: Number of inversions in an array are minimum when the given array is sorted. As the necessary condition for an inversion is arr[i]>arr[j] and i
Q: How many inversions are there in the array arr = {1,5,4,2,3}?
A. 0
B. 3
C. 4
D. 5
Solution: The necessary condition for an inversion is arr[i]>arr[j] and i
Q: Which of the following form inversion in the array arr = {1,5,4,2}?
A. (5,4), (5,2)
B. (5,4), (5,2), (4,2)
C. (1,5), (1,4), (1,2)
D. (1,5)
Solution: The necessary condition for an inversion is arr[i]>arr[j] and i
Q: Choose the correct function from the following which determines the number of inversions in an array?
A. int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i ; j < n; j++) if (arr[i] >= arr[j]) count++; return count; }
B. int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] > arr[j]) count++; return count; }
C. int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] > arr[j]) count++; return count + 1; }
D. int InvCount(int arr[], int n) { int count = 0; for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++) if (arr[i] < arr[j]) count++; return count + 1; }
Solution: To determine the number of inversions we apply a nested loop and compare the value of each element with all the elements present after it. Then the count of number of inversions is counted and returned to the main function.
Q: What is the time complexity of the following code that determines the number of inversions in an array?

int InvCount(int arr[], int n) 
{ 
	int count = 0; 
	for (int i = 0; i < n - 1; i++) 
		for (int j = i + 1; j < n; j++) 
			if (arr[i] > arr[j]) 
				count++; 
 
	return count; 
}
A. O(n)
B. O(n log n)
C. O(n2)
D. O(log n)
Solution: The time complexity of the given code is O(n2). It is due to the presence of nested loop.
Q: The time complexity of the code that determines the number of inversions in an array using merge sort is lesser than that of the code that uses loops for the same purpose.
A. true
B. false
C. ....
D. ....
Solution: The time complexity of the code that determines the number of inversions in an array using merge sort is O(n log n) which is lesser than the time complexity taken by the code that uses loops.

You Have Score    /10